import React, { Component } from "react";
import Editor from "@/utils/Editor";
import { Icon, Button, Row, Col, Form } from "antd";
import styles from "./index.less";
import { connect } from "dva/index";
import { message } from "antd/lib/index";
import UserSession from "@/UserSession";
import AnswerUtils from "@/utils/answerUtils/answerUtils";
import jsonDataUtils from "@/utils/jsonDataUtils";
import { knowledgeDialogActionNodeInfo } from '@/services/dialog';
@connect(({ dialogCommon, loading }) => ({
	dialogCommon,
	loading: loading.models.data,
}))
class ActionReply extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			dialogId: props.dialogId,
			nodeId: props.nodeId,
			stage: props.stage,

			content: "",
			stageId: "",
		};
	}
	componentDidMount() {
		this.getInit();
	}

	getInit = async () => {
		const { nodeId, dialogId, stage } = this.state;
		const { dispatch } = this.props;
		if (UserSession.isSystemKnowledge()) {
			let res = await knowledgeDialogActionNodeInfo({ dialogId, nodeId });
			this.setState({ stageId: res.id }, () => {
				//处理答案字段
				let answerObj = {
					isDefault: true,
					content: res?.reply?.content,
					id: res.id,
					slotValues: [],
					userTagIdList: [],
					contentType: res?.reply?.contentType ? res?.reply?.contentType + '' : '0',    //答案内容类型 0=纯文本类型, 1=富文本类型, 2=跳转卡片 默认为0
				};
				if (jsonDataUtils.isJsonString(res?.reply?.content)) {
					answerObj.cardVo = JSON.parse(res?.reply?.content)
				};
				setTimeout(() => {
					this.AnswerUtils.setAnswers(answerObj)
				}, 300)
			});
		} else {
			dispatch({
				type: "dialogCommon/getActionData",
				payload: {
					dialogId: dialogId,
					nodeId: nodeId,
					urlPrefix: UserSession.isSystemKnowledge() ? 1 : null,
				},
				callback: (res) => {
					this.setState({ stageId: res.id }, () => {
						//处理答案字段
						let answerObj = {
							isDefault: true,
							content: res?.reply?.content,
							id: res.id,
							slotValues: [],
							userTagIdList: [],
							contentType: res?.reply?.contentType ? res?.reply?.contentType + '' : '0',    //答案内容类型 0=纯文本类型, 1=富文本类型, 2=跳转卡片 默认为0
						};
						if (jsonDataUtils.isJsonString(res?.reply?.content)) {
							answerObj.cardVo = JSON.parse(res?.reply?.content)
						};
						setTimeout(() => {
							this.AnswerUtils.setAnswers(answerObj)
						}, 300)
					});
				},
			});
		}
	};
	save = async () => {
		const { nodeId, dialogId, stageId, stage } = this.state;
		const { dispatch } = this.props;
		const answerResult = await this.AnswerUtils.getAnswerResult();
		if (answerResult.flag) {
			let answer = answerResult.result.content;
			let contentType = answerResult.result.contentType;
			dispatch({
				type: "dialogCommon/saveStage",
				payload: {
					id: stageId ? stageId : dialogId + "-" + nodeId,
					field: "reply",
					reply: {
						content: answer,
						exit: "",
						setTime: new Date().getTime(),
						contentType
					},
					stage,
				},
				callback: (res) => {
					message.info(res.msg);
					this.props.closeDrawer();
				},
			});
		} else {
			if (answerResult.message) {
				message.error(answerResult.message)
			}
			return false
		}
	};

	render() {
		return (
			<div tt='1'>
				<Row>
					<Col xs={24}>
						<div className={styles.title}>
							<span>回复内容</span>
						</div>
					</Col>
				</Row>
				<div
					className='fuwenben'
					style={{
						lineHeight: "20px",
						width: 640,
						zIndex: 1,
						paddingLeft: "30px",
						marginTop: "15px",
					}}
				>
					<Form>
						<AnswerUtils form={this.props.form} onRef={ref => this.AnswerUtils = ref} />
					</Form>
				</div>

				<div
					style={{
						position: 'absolute',
						right: 0,
						bottom: 0,
						width: '100%',
						borderTop: '1px solid #e9e9e9',
						padding: '10px 16px',
						background: '#fff',
						textAlign: 'right',
					}}
				>
					<Button onClick={this.props.closeDrawer} style={{ marginRight: 8 }}>
						取消
					</Button>
					<Button onClick={this.save} disabled={UserSession.isSystemKnowledge()} type="primary">
						确定
					</Button>
				</div>
			</div>
		);
	}
}

export default Form.create()(ActionReply);
